package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    PermissionService permissionService;
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=permissionService.findPage(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            return new Result(false, "添加失败");
        }
        return new Result(true, "添加成功");
    }
    @RequestMapping("/delete")
    public Result add(Integer id){
        try {
            permissionService.delete(id);
        } catch (Exception e) {
            return new Result(false, "删除失败");
        }
        return new Result(true, "删除成功");
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
           Permission permission= permissionService.findById(id);
        return new Result(true, "",permission);
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.update(permission);
                    return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("修改失败");
    }
}
